package com.lzb.watermarkcamera.activity;

import android.os.Bundle;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.lzb.watermarkcamera.BaseAc;
import com.lzb.watermarkcamera.Content;
import com.lzb.watermarkcamera.R;
import com.lzb.watermarkcamera.adapter.AlbumChildAdapter;
import com.lzb.watermarkcamera.adapter.PhoneAlbumAdapter;
import com.lzb.watermarkcamera.basic.utils.FileP2pUtil;
import com.lzb.watermarkcamera.basic.utils.FileUtil;
import com.lzb.watermarkcamera.basic.utils.RxUtil;
import com.lzb.watermarkcamera.bean.PhoneAlbumBean;
import com.lzb.watermarkcamera.databinding.ActivityRecentDeleteBinding;
import com.lzb.watermarkcamera.utils.MyMediaLoader;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.rxjava3.core.ObservableEmitter;

public class RecentDeleteActivity extends BaseAc<ActivityRecentDeleteBinding> implements View.OnClickListener, OnItemClickListener, OnItemChildClickListener {

    private PhoneAlbumAdapter mPhoneAlbumAdapter;
    private List<String> listSel = new ArrayList<>();
    private int totalIndex;
    private boolean hasSelectAll;


    @Override
    protected int onCreate() {
        return R.layout.activity_recent_delete;
    }

    @Override
    protected void initView() {
        mDataBinding.ivRecentDeleteBack.setOnClickListener(view -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.llAllSel.setOnClickListener(this);
        mDataBinding.tvDelete.setOnClickListener(this);
        mDataBinding.tvRestore.setOnClickListener(this);

        mDataBinding.rvPictureList.setLayoutManager(new LinearLayoutManager(mContext));
        mPhoneAlbumAdapter = new PhoneAlbumAdapter();
        mDataBinding.rvPictureList.setAdapter(mPhoneAlbumAdapter);
        mPhoneAlbumAdapter.setEdit(true);
        mPhoneAlbumAdapter.addChildClickViewIds(R.id.llPhoneAlbumSelector);
        mPhoneAlbumAdapter.setOnItemClickListener(this);
        mPhoneAlbumAdapter.setOnItemChildClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.llAllSel) {//全选
            hasSelectAll = !hasSelectAll;
            setHasSelectAll(hasSelectAll);
        } else if (id == R.id.tvDelete) {//删除
            if (listSel.size() == 0) return;
            for (String path : listSel) {
                FileUtils.delete(path);
            }
            ToastUtils.showShort(getString(R.string.delete_suc));
            listSel.clear();
            getData();
        } else if (id == R.id.tvRestore) {//恢复
            if (listSel.size() == 0) return;
            for (String path : listSel) {
                FileP2pUtil.copyPrivateImgToPublic(mContext, path);
                FileUtils.delete(path);
            }
            ToastUtils.showShort(getString(R.string.restore_suc));
            listSel.clear();
            getData();
        }
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (adapter instanceof AlbumChildAdapter) {
            AlbumChildAdapter albumChildAdapter = (AlbumChildAdapter) adapter;
            PhoneAlbumBean.ClassBean item = albumChildAdapter.getItem(position);
            item.setSelected(!item.isSelected());
            albumChildAdapter.notifyDataSetChanged();

            boolean isSelectAll = true;
            for (PhoneAlbumBean.ClassBean classBean : albumChildAdapter.getData()) {
                if (!classBean.isSelected()) {
                    isSelectAll = false;
                    break;
                }
            }
            PhoneAlbumBean phoneAlbumBean = albumChildAdapter.getPhoneAlbumBean();
            phoneAlbumBean.setSelected(isSelectAll);
            mPhoneAlbumAdapter.notifyDataSetChanged();

            if (item.isSelected()) {
                listSel.add(item.getPath());
            } else {
                listSel.remove(item.getPath());
            }
        }
        hasSelectAll = listSel.size() == totalIndex;
        mDataBinding.ivAllSel.setImageResource(hasSelectAll ? R.drawable.axz2 : R.drawable.awxz);
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (view.getId() == R.id.llPhoneAlbumSelector) {
            PhoneAlbumBean item = mPhoneAlbumAdapter.getItem(position);
            item.setSelected(!item.isSelected());
            for (PhoneAlbumBean.ClassBean classBean : item.getClassBeanList()) {
                classBean.setSelected(item.isSelected());
            }
            mPhoneAlbumAdapter.notifyDataSetChanged();

            if (item.isSelected()) {
                for (PhoneAlbumBean.ClassBean classBean : item.getClassBeanList()) {
                    listSel.add(classBean.getPath());
                }
            } else {
                for (PhoneAlbumBean.ClassBean classBean : item.getClassBeanList()) {
                    listSel.remove(classBean.getPath());
                }
            }
        }
        hasSelectAll = listSel.size() == totalIndex;
        mDataBinding.ivAllSel.setImageResource(hasSelectAll ? R.drawable.axz2 : R.drawable.awxz);
    }

    @Override
    protected void initData() {
        getData();
    }

    private void getData() {
        RxUtil.create(new RxUtil.Callback<List<PhoneAlbumBean>>() {
            @Override
            public void doBackground(ObservableEmitter<List<PhoneAlbumBean>> emitter) {
                List<PhoneAlbumBean> phoneAlbumBeans = MyMediaLoader.getInstance().loadPicData(true, Content.recentDelete);
                emitter.onNext(phoneAlbumBeans);
            }

            @Override
            public void accept(List<PhoneAlbumBean> phoneAlbumBeanList) {
                if (phoneAlbumBeanList == null || phoneAlbumBeanList.size() == 0) {
                    mDataBinding.flPictureNoData.setVisibility(View.VISIBLE);
                    mDataBinding.rvPictureList.setVisibility(View.GONE);
                } else {
                    mDataBinding.flPictureNoData.setVisibility(View.GONE);
                    mDataBinding.rvPictureList.setVisibility(View.VISIBLE);
                }

                mPhoneAlbumAdapter.setList(phoneAlbumBeanList);
                getTotalCount();
            }
        });
    }

    private void getTotalCount() {
        totalIndex = 0;
        for (PhoneAlbumBean phoneAlbumBean : mPhoneAlbumAdapter.getData()) {
            totalIndex = phoneAlbumBean.getClassBeanList().size() + totalIndex;
        }
    }

    private void setHasSelectAll(boolean hasSelectAll) {
        List<PhoneAlbumBean.ClassBean> classBeanList = new ArrayList<>();
        for (PhoneAlbumBean datum : mPhoneAlbumAdapter.getData()) {
            datum.setSelected(hasSelectAll);
            classBeanList.addAll(datum.getClassBeanList());

            for (PhoneAlbumBean.ClassBean classBean : datum.getClassBeanList()) {
                classBean.setSelected(hasSelectAll);
            }
        }

        List<String> list = new ArrayList<>();
        for (PhoneAlbumBean.ClassBean classBean : classBeanList) {
            list.add(classBean.getPath());
        }
        listSel.clear();

        if (hasSelectAll) {
            list.addAll(list);
            mDataBinding.ivAllSel.setImageResource(R.drawable.axz2);
        } else {
            mDataBinding.ivAllSel.setImageResource(R.drawable.awxz);
        }
        mPhoneAlbumAdapter.notifyDataSetChanged();
    }

}